#!/usr/bin/python3
# -*- coding: utf-8 -*-
import requests
import sys
import base64
import re

# 用户配置，用户需要自己修改
username = "12345"
passwd = "12345"
# 用户配置结束

userip = ""
usermac = ""
host_addr = "http://10.2.24.32:8080/byod"
referer = 'http://www.hnu.edu.cn/'
base_header = {
    'Accept': 'text/html,application/xhtml\'+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    'Host': '10.2.24.32:8080',
    'Referer': referer,
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'
}
# 获取网络分配的usermac和userip
while (userip == "" or usermac == ""):
    req_addr = "http://www.hnu.edu.cn/"
    ret = requests.get(req_addr, headers = base_header)
    userip = re.findall(r"userip=(.+?)&", ret.text)[-1]
    usermac = re.findall(r"usermac=(.+?)&", ret.text)[-1]
print("[0] get ip and mac: ip = ", userip, ", mac = ", usermac)

# 第一层认证，获取sessionid等cookie信息
# 主要得到JSESSIONID
mac_req_addr = host_addr + "?usermac=" + usermac + "&userip=" + userip + "&userurl=http://www.hnu.edu.cn/&original=http://www.hnu.edu.cn/"
ret = requests.get(mac_req_addr, headers = base_header)
cookie_t = ret.headers['Set-Cookie']
cookie_dict = {
    'JSESSIONID': 'abb',
    'oam.Flash.RENDERMAP.TOKEN': 'abb'
}
cookie_dict['JSESSIONID'] = re.findall(r"JSESSIONID=(.+?);", cookie_t)[0]
cookie_dict['oam.Flash.RENDERMAP.TOKEN'] = re.findall(r"oam\.Flash\.RENDERMAP\.TOKEN=(.+?);", cookie_t)[-1]
cookie = "JSESSIONID=" + cookie_dict['JSESSIONID'] + ";oam.Flash.RENDERMAP.TOKEN=" + cookie_dict['oam.Flash.RENDERMAP.TOKEN']
print("[1] get cookie ok: cookie = ", cookie)

# 第二层认证，得到第一个viewstate信息
index_addr = host_addr + "/templatePage/20160930191146463/guestRegister.jsf"
index_header = base_header
index_header.update({
    'Cache-Control': 'max-age=0',
    'Cookie': cookie
})
ret = requests.get(index_addr, headers=index_header)
cookie_t = ret.headers['Set-Cookie']
cookie_dict['JSESSIONID'] = re.findall(r"JSESSIONID=(.+?);", cookie_t)[0]
cookie_dict['oam.Flash.RENDERMAP.TOKEN'] = re.findall(r"oam\.Flash\.RENDERMAP\.TOKEN=(.+?);", cookie_t)[-1]
cookie = "JSESSIONID=" + cookie_dict['JSESSIONID'] + ";oam.Flash.RENDERMAP.TOKEN=" + cookie_dict['oam.Flash.RENDERMAP.TOKEN']
# print("[2] ---- header: ", ret.headers)
# print(ret.text)
viewstate_s = re.findall(r"id=\"javax.faces.ViewState\" value=\"(.+?)\"", ret.text)
viewstate = viewstate_s[-1]
print("[2] get first viewstate ok: viewstate = ", viewstate)

# 第三次认证，得到获取完整的viewstate信息的资格
jsessionid = cookie_dict['JSESSIONID']
viewstate_addr = host_addr + "/index.xhtml;jsessionid=" + jsessionid
# print("---", viewstate_addr)
viewstate_header = base_header
viewstate_header.update({
    'Content-Length': '269',
    'Cache-Control': 'max-age=0',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Cookie': cookie,
    'Origin': 'http://10.2.24.32:8080'
})
viewstate_data = {
    'wlannasid':'',
    'usermac': usermac,
    'userurl': 'http://www.hnu.edu.cn/',
    'userip': userip,
    'ssid':'',
    'btn': '',
    'j_id_3_SUBMIT': '1',
    'javax.faces.ViewState': viewstate
}
ret = requests.post(viewstate_addr, headers=viewstate_header, data=viewstate_data)
cookie_t = ret.headers['Set-Cookie']
cookie_dict['oam.Flash.RENDERMAP.TOKEN'] = re.findall(r"oam\.Flash\.RENDERMAP\.TOKEN=(.+?);", cookie_t)[-1]
cookie = "JSESSIONID=" + cookie_dict['JSESSIONID'] + ";oam.Flash.RENDERMAP.TOKEN=" + cookie_dict['oam.Flash.RENDERMAP.TOKEN']
# print("[3] ---- header: ", ret.headers)
print("[3] post index.xhtml ok")


# 第四层认证，得到完整viewstate信息
index_addr = host_addr + "/templatePage/20160930191146463/guestRegister.jsf"
index_header = base_header
index_header.update({
    'Cache-Control': 'max-age=0',
    'Cookie': cookie
})
ret = requests.get(index_addr, headers=index_header)
cookie_t = ret.headers['Set-Cookie']
cookie_dict['oam.Flash.RENDERMAP.TOKEN'] = re.findall(r"oam\.Flash\.RENDERMAP\.TOKEN=(.+?);", cookie_t)[-1]
cookie = "JSESSIONID=" + cookie_dict['JSESSIONID'] + ";oam.Flash.RENDERMAP.TOKEN=" + cookie_dict['oam.Flash.RENDERMAP.TOKEN']
# print("[4] ---- header: ", ret.headers)
# print(ret.text)
viewstate_s = re.findall(r"id=\"javax.faces.ViewState\" value=\"(.+?)\"", ret.text)
viewstate = viewstate_s[-1]
print("[4] get last viewstate ok: viewstate = ", viewstate)

# 第五层验证，提交完整的账号信息
index_addr = host_addr + "/templatePage/20160930191146463/guestRegister.jsf"
index_header = base_header
index_header.update({
    'Content-Length': '1057',
    'Cache-Control': 'max-age=0',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Cookie': cookie,
    'Origin': 'http://10.2.24.32:8080',
    'X-Requested-With': 'XMLHttpRequest'
})
post_data={
	'javax.faces.partial.ajax': 'true',
	'javax.faces.source': 'mainForm:j_id_r',
	'javax.faces.partial.execute': 'mainForm',
	'javax.faces.partial.render': 'mainForm:error mainForm:forResetPwd',
	'mainForm:j_id_r': 'mainForm:j_id_r',
	'mainForm:forResetPwd': '',
	'userName': '',
	'userPwd': '',
	'userDynamicPwd': '',
	'userDynamicPwdd': '',
	'mainForm:userNameLogin': username,
	'mainForm:serviceSuffixLogin': '',
	'mainForm:passwordLogin': base64.b64encode(passwd.encode()),
	'mainForm:userDynamicPwd': '',
	'mainForm:userDynamicPwdd': '',
	'mainForm_SUBMIT': '1',
	'javax.faces.ViewState': viewstate
}
ret=requests.post(index_addr, data=post_data, headers=index_header)

print("[5] login successfully! Happy working!")
